package pearson.automation.forms.courseDetails.module;

import java.util.HashMap;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import pearson.automation.assets.AssetWithOutRollOver;
import pearson.automation.forms.RemoveConfirmationForm;
import framework.elements.Button;
import framework.elements.Label;

/**
 * 
 * This form represents DropBox module's content part
 * 
 */
public class DropBoxForm extends SectionForm {

    private final static Label identifyer = new Label(By.cssSelector("h1[class*='module-title']"), "Drop Box");
    private final static String[] defaultAssetNames = {
        "Curriculum Guide", 
        "Student Outcomes", 
        "Course Schedule",
        "Student Input Documentation" };

    private HashMap<String, AssetWithOutRollOver> assets;

    public DropBoxForm() {
    super(identifyer.getLocator(), identifyer.getName());
    Assert.assertEquals(identifyer.getText(), identifyer.getName(), "Module title is wrong");
    assets = new HashMap<String, AssetWithOutRollOver>();
    title = identifyer;
    for (String assetName : defaultAssetNames) {
        assets.put(assetName, new AssetWithOutRollOver(String.format(assetXpathTemplate, assetName), true));
    }
    }

    public void assertDefaultAssetsPresents() {
    for (String assetName : defaultAssetNames) {
        Assert.assertTrue(assets.get(assetName).isPresent(), String.format("Asset %1$s is absent", assetName));
    }
    }

    public void addChapterForEachAsset(String suffix) {
    title.clickAndWait();
    for (String assetName : defaultAssetNames) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        assets.get(assetName).click(true);
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        WebElement activeElement = browser.getDriver().switchTo().activeElement();
        activeElement.sendKeys(String.format("%1$s %2$s", assetName, suffix), Keys.ENTER);
        title.click();
    }

    }

    public void assertAssetChaptersPresent(String suffix) {
    for (String assetName : defaultAssetNames) {
        assertChapterPresent(String.format("%1$s %2$s", assetName, suffix));
    }
    }

    public String[] getAssetNames() {
    return defaultAssetNames.clone();
    }

    public void deleteAsset(String assetName) {
    Label assetDataRef = new Label(By.xpath(String.format(
            "//div[contains(@class,'section_content')]"
          + "//div[contains(@class,'mceNonEditable')]"
          + "//p[contains(text(),'%s')]/../..", assetName)), "Asset");
    assetDataRef.waitAndAssertIsPresent();
    String data = assetDataRef.getElement().getAttribute("data-content-ref");
    assets.get(assetName).click(true);
    browser.getDriver().switchTo().frame(0);
    Button deleteAsset = new Button(By.xpath(String.format(
            assetDeleteXpathTemplate, data)), assetName);
    deleteAsset.clickAndWait();
    browser.getDriver().switchTo().defaultContent();
    new RemoveConfirmationForm().clickYes();
    title.clickAndWait();
    }

}
